package src.com.unapec.seguridad.EjemploFinal;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Rectangle;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;

import src.unapec.seguridad.ejemplos.EncriptacionString.Encriptacion;
import src.unapec.seguridad.ejemplos.Utils.Key;

import com.mysql.jdbc.ResultSet;

public class PantallaPrincipal extends JFrame {

	private static final long serialVersionUID = 1L;
	
	private String[] fields = {"Last Name","Name","id","password","confirmation"};
	
	private JOptionPane pane = new JOptionPane();

	private JPanel jContentPane = null;

	private JPanel jPanel = null;

	private JPanel jPanel1 = null;

	private JPanel jPanel2 = null;

	private JPanel jPanel3 = null;

	private JButton jButton = null;

	private JTextField jTextField = null;

	private JLabel jLabel = null;

	private JLabel jLabel1 = null;

	private JPasswordField jPasswordField = null;

	private DefaultTableModel tableModel = new DefaultTableModel();
	
	private JLabel jLabel2 = null;

	private JPanel jPanel4 = null;

	private BDManager bdmanager = new BDManager();
	
	private JScrollPane jScrollPane = null;

	private JButton jButton1 = null;

	private JTable jTable = null;

	private JLabel jLabel3 = null;

	private JLabel jLabel4 = null;

	private JTextField jTextField1 = null;

	private JPasswordField jPasswordField1 = null;

	private JButton jButton2 = null;

	private JPanel jPanel5 = null;

	private JLabel jLabel5 = null;

	/**
	 * This is the default constructor
	 */
	public PantallaPrincipal() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(660, 392);
		this.setContentPane(getJContentPane());
		this.setTitle("Main Screen");
		addColumns();
		jTable.setModel(tableModel);
		
	}

	private void addColumns(){
		
		for (int i = 0; i < fields.length; i++) {
			
			tableModel.addColumn(fields[i]);
			
		}
		
	}
	
	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getJPanel(), null);
			jContentPane.add(getJPanel1(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			jPanel = new JPanel();
			jPanel.setLayout(null);
			jPanel.setBounds(new Rectangle(8, 6, 415, 350));
			jPanel.add(getJPanel2(), null);
			jPanel.add(getJPanel3(), null);
			jPanel.add(getJPanel4(), null);
		}
		return jPanel;
	}

	/**
	 * This method initializes jPanel1	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			jLabel4 = new JLabel();
			jLabel4.setBounds(new Rectangle(16, 55, 63, 15));
			jLabel4.setText("Password:");
			jLabel3 = new JLabel();
			jLabel3.setBounds(new Rectangle(16, 22, 65, 15));
			jLabel3.setText("Username:");
			jPanel1 = new JPanel();
			jPanel1.setLayout(null);
			jPanel1.setBounds(new Rectangle(426, 6, 216, 349));
			jPanel1.setBorder(BorderFactory.createTitledBorder(null, "Login", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
			jPanel1.add(jLabel3, null);
			jPanel1.add(jLabel4, null);
			jPanel1.add(getJTextField1(), null);
			jPanel1.add(getJPasswordField1(), null);
			jPanel1.add(getJButton2(), null);
			jPanel1.add(getJPanel5(), null);
		}
		return jPanel1;
	}

	/**
	 * This method initializes jPanel2	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel2() {
		if (jPanel2 == null) {
			jPanel2 = new JPanel();
			jPanel2.setLayout(new GridBagLayout());
			jPanel2.setBounds(new Rectangle(0, 0, 0, 0));
		}
		return jPanel2;
	}

	/**
	 * This method initializes jPanel3	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel3() {
		if (jPanel3 == null) {
			jLabel2 = new JLabel();
			jLabel2.setBounds(new Rectangle(13, 79, 363, 23));
			jLabel2.setHorizontalAlignment(SwingConstants.CENTER);
			jLabel2.setText("Password: XXXXXXXX");
			jLabel1 = new JLabel();
			jLabel1.setBounds(new Rectangle(206, 16, 57, 15));
			jLabel1.setText("Enter CN:");
			jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
			jLabel = new JLabel();
			jLabel.setBounds(new Rectangle(8, 10, 85, 24));
			jLabel.setHorizontalAlignment(SwingConstants.CENTER);
			jLabel.setText("Enter UN:");
			jPanel3 = new JPanel();
			jPanel3.setLayout(null);
			jPanel3.setBounds(new Rectangle(14, 14, 387, 116));
			jPanel3.add(getJButton(), null);
			jPanel3.add(getJTextField(), null);
			jPanel3.add(jLabel, null);
			jPanel3.add(jLabel1, null);
			jPanel3.add(getJPasswordField(), null);
			jPanel3.add(jLabel2, null);
		}
		return jPanel3;
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setBounds(new Rectangle(9, 43, 368, 28));
			jButton.setText("Show Password");
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
				
					if((getJTextField().getText().length() <= 0) || (String.valueOf(getJPasswordField().getPassword()).length() <= 0 )){
						
						pane.showMessageDialog(null, "UN or CN field is empty", "Error", JOptionPane.ERROR_MESSAGE);
						getJTextField().setText("");
						getJPasswordField().setText("");
						
					}else{
						
						String password = bdmanager.getPassword(getJTextField().getText(), String.valueOf(getJPasswordField().getPassword()));
						
						if(password == null){
							
							pane.showMessageDialog(null, "Invalid UN or CN", "Error", JOptionPane.ERROR_MESSAGE);
							getJTextField().setText("");
							getJPasswordField().setText("");
							jLabel2.setText("Password:");
							
						}else{
						
						jLabel2.setText("Password: "+new Encriptacion().decrypt(password, new Key().getKeys(1)));
												
						}
					}
					
				
				}
			});
		}
		return jButton;
	}

	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField() {
		if (jTextField == null) {
			jTextField = new JTextField();
			jTextField.setBounds(new Rectangle(96, 8, 106, 30));
		}
		return jTextField;
	}

	/**
	 * This method initializes jPasswordField	
	 * 	
	 * @return javax.swing.JPasswordField	
	 */
	private JPasswordField getJPasswordField() {
		if (jPasswordField == null) {
			jPasswordField = new JPasswordField();
			jPasswordField.setBounds(new Rectangle(273, 8, 106, 30));
		}
		return jPasswordField;
	}

	/**
	 * This method initializes jPanel4	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel4() {
		if (jPanel4 == null) {
			jPanel4 = new JPanel();
			jPanel4.setLayout(null);
			jPanel4.setBounds(new Rectangle(18, 143, 382, 194));
			jPanel4.setBorder(BorderFactory.createTitledBorder(null, "Administrator", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
			jPanel4.add(getJScrollPane(), null);
			jPanel4.add(getJButton1(), null);
		}
		return jPanel4;
	}

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
			jScrollPane.setBounds(new Rectangle(13, 19, 358, 145));
			jScrollPane.setViewportView(getJTable());
		}
		return jScrollPane;
	}

	
	/**
	 * This method initializes jButton1	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setBounds(new Rectangle(16, 170, 351, 16));
			jButton1.setText("Show all users");
			jButton1.addActionListener(new ActionListener(){
				
				public void actionPerformed(ActionEvent e){
					
					tableModel = new DefaultTableModel();
					addColumns();
					jTable.setModel(tableModel);
					
					
					java.sql.ResultSet rs = bdmanager.getData();
					String[] values = new String[5];					
					
					try {
						
						while(rs.next()){
							
							for (int j = 1; j < 6; j++) {
								
								values[j-1] = rs.getString(j);
								
							}
							
							tableModel.addRow(values);
							values = new String[5];
							
						}
					
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
						
				}
					
			});
		}
		return jButton1;
	}

	/**
	 * This method initializes jTable	
	 * 	
	 * @return javax.swing.JTable	
	 */
	private JTable getJTable() {
		if (jTable == null) {
			jTable = new JTable();
		}
		return jTable;
	}

	/**
	 * This method initializes jTextField1	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField1() {
		if (jTextField1 == null) {
			jTextField1 = new JTextField();
			jTextField1.setBounds(new Rectangle(83, 19, 103, 29));
		}
		return jTextField1;
	}

	/**
	 * This method initializes jPasswordField1	
	 * 	
	 * @return javax.swing.JPasswordField	
	 */
	private JPasswordField getJPasswordField1() {
		if (jPasswordField1 == null) {
			jPasswordField1 = new JPasswordField();
			jPasswordField1.setBounds(new Rectangle(83, 54, 103, 29));
		}
		return jPasswordField1;
	}

	/**
	 * This method initializes jButton2	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton2() {
		if (jButton2 == null) {
			jButton2 = new JButton();
			jButton2.setBounds(new Rectangle(23, 96, 171, 36));
			jButton2.setText("Enter");
		}
		return jButton2;
	}

	/**
	 * This method initializes jPanel5	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel5() {
		if (jPanel5 == null) {
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.gridy = 0;
			jLabel5 = new JLabel();
			jLabel5.setText("IMAGE");
			jPanel5 = new JPanel();
			jPanel5.setLayout(new GridBagLayout());
			jPanel5.setBounds(new Rectangle(19, 159, 181, 166));
			jPanel5.setBorder(BorderFactory.createLineBorder(SystemColor.activeCaption, 5));
			jPanel5.add(jLabel5, gridBagConstraints);
		}
		return jPanel5;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
